/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ScriptStatusWindow.java
 *
 * Created on 31.10.2009, 1:17:16
 */
package trim.gui.testing;

import java.awt.Frame;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import javax.swing.SwingUtilities;
import trim.gui.testing.export.ExportManager;
import trim.gui.testing.models.ResultListTableModel;
import trim.gui.testing.script.ImportData;
import trim.scripting.engine.GroovyScriptEngine;
import trim.scripting.engine.IScriptEngine;
import trim.scripting.engine.ScriptException;
import trim.scripting.interfaces.IPlaginManager;
import trim.scripting.interfaces.IStateWindow;

/**
 *
 * @author Дмитрий
 */
public class ScriptStatusWindow extends javax.swing.JDialog implements IStateWindow
{
Thread scriptThread;
  /** Creates new form ScriptStatusWindow */
  public ScriptStatusWindow(java.awt.Frame parent,boolean modal)
  {
    super(parent,modal);
    initComponents();
  }

  public ScriptStatusWindow(java.awt.Dialog parent,String text,ExportManager exportManager,
    ImportData importData,ResultListTableModel resultListTableModel,PlaginManager pm)
  {
    super(parent,true);
    initComponents();
    scriptThread=new ScriptThread(text,exportManager,
      importData,resultListTableModel,this,pm);
    scriptThread.start();
    progressBar.setVisible(false);
    this.setLocationRelativeTo(parent);
    setVisible(true);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    messageLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();
    jScrollPane1 = new javax.swing.JScrollPane();
    logTextArea = new javax.swing.JTextArea();
    btStop = new javax.swing.JButton();
    jButton1 = new javax.swing.JButton();

    messageLabel.setText("Script starting");

    logTextArea.setColumns(20);
    logTextArea.setEditable(false);
    logTextArea.setRows(5);
    jScrollPane1.setViewportView(logTextArea);

    btStop.setText("Stop");
    btStop.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btStopActionPerformed(evt);
      }
    });

    jButton1.setText("Close");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(btStop)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 286, Short.MAX_VALUE)
        .addComponent(jButton1))
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
      .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
      .addComponent(messageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(messageLabel)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(btStop)
          .addComponent(jButton1)))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void btStopActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btStopActionPerformed
    {//GEN-HEADEREND:event_btStopActionPerformed
scriptThread.stop();
scriptThread.destroy();
}//GEN-LAST:event_btStopActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
      setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

 
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btStop;
  private javax.swing.JButton jButton1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea logTextArea;
  private javax.swing.JLabel messageLabel;
  private javax.swing.JProgressBar progressBar;
  // End of variables declaration//GEN-END:variables

  @Override
  public void setText(String text)
  {
    messageLabel.setText(text);
  }

  @Override
  public void setProgressBarVisible(boolean isVisible)
  {
    progressBar.setVisible(isVisible);
  }

  public void setLogVisible(boolean isVisible)
  {
  }

  public void setProgressBarMinimum(int minimum)
  {
    progressBar.setMinimum(minimum);
  }

  public void setProgressBarMaximum(int maximum)
  {
    progressBar.setMaximum(maximum);
  }

  public void setProgressBarValue(int value)
  {
    progressBar.setValue(value);
  }

  public void logPrint(final String note)
  {
    SwingUtilities.invokeLater(new Runnable()
    {

      public void run()
      {
        logTextArea.append(note);
      }
    });
  }

  public void logPrintln(final String note)
  {
    SwingUtilities.invokeLater(new Runnable()
    {

      public void run()
      {
        logTextArea.append(note+"\n");
      }
    });

  }

  private class ScriptThread extends Thread
  {

    private String text;
    private ExportManager exportManager;
    private ImportData importData;
    private ResultListTableModel resultListTableModel;
    private ScriptStatusWindow stateWindow;
    private PlaginManager pm;

    private ScriptThread(String text,ExportManager exportManager,
      ImportData importData,ResultListTableModel resultListTableModel,
      ScriptStatusWindow aThis,PlaginManager pm)
    {
      this.text=text;
      this.exportManager=exportManager;
      this.importData=importData;
      this.resultListTableModel=resultListTableModel;
      this.stateWindow=aThis;
      this.pm=pm;
    }

    @Override
    public void run()
    {
      super.run();
      try
      {
        IScriptEngine se=new GroovyScriptEngine();
        se.run(text,exportManager,
          importData,resultListTableModel,stateWindow,pm);
      }catch(ScriptException ex)
      {
        final ByteArrayOutputStream os=new ByteArrayOutputStream(1024);
        ex.printStackTrace(new PrintStream(os));
        SwingUtilities.invokeLater(new Runnable()
        {

          public void run()
          {
            stateWindow.logPrintln(new String(os.toByteArray()));
          }
        });
      }
      SwingUtilities.invokeLater(new Runnable()
      {

        public void run()
        {
          stateWindow.setText("finish");
        }
      });

    }
  }
}
